package com.collectplus.express.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

import com.collectplus.express.R;

@SuppressLint("AppCompatCustomView")
public class DeleteEditText extends EditText implements OnFocusChangeListener, TextWatcher {

	private Drawable mCloseDrawable; // 关闭
	private Drawable mEmptyDrawable; // 空白-占位
	private boolean hasFoucs;

	public DeleteEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public DeleteEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public DeleteEditText(Context context) {
		super(context);
		init();
	}

	private void init() {
		mCloseDrawable = getResources().getDrawable(R.drawable.icon_clear);
		mCloseDrawable.setBounds(0, 0, mCloseDrawable.getIntrinsicWidth(), mCloseDrawable.getIntrinsicHeight());
		mEmptyDrawable = getResources().getDrawable(R.drawable.icon_clear_empty);
		mEmptyDrawable.setBounds(0, 0, mEmptyDrawable.getIntrinsicWidth(), mEmptyDrawable.getIntrinsicHeight());
		// setBackgroundResource(R.drawable.edit_bg);
		setClearIconVisible(false);
		setOnFocusChangeListener(this);
		addTextChangedListener(this);

	}

	protected void setClearIconVisible(boolean visible) {
		Drawable right = visible ? mCloseDrawable : mEmptyDrawable;
		setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (getCompoundDrawables()[2] != null) {
				boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
				if (touchable) {
					this.setText(null);
				}
			}
		}

		return super.onTouchEvent(event);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		this.hasFoucs = hasFocus;
		if (hasFocus) {
			setClearIconVisible(getText().length() > 0);
		} else {
			setClearIconVisible(false);
		}
		if (mOnFocusChangeListener != null)
			mOnFocusChangeListener.onFocusChange(v, hasFocus);
	}

	private MyOnFocusChangeListener mOnFocusChangeListener;

	public void setOnFocusChange(MyOnFocusChangeListener lis) {
		mOnFocusChangeListener = lis;
	}

	public interface MyOnFocusChangeListener {
		void onFocusChange(View v, boolean hasFocus);
	}

	@Override
	public void afterTextChanged(Editable arg0) {

	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
		if (hasFoucs) {
			setClearIconVisible(text.length() > 0);
		}
	}

}
